package edu.ihs.lhpy.action;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import edu.ihs.lhpy.base.BaseAction;
import edu.ihs.lhpy.domain.PageBean;
import edu.ihs.lhpy.domain.Privilege;
import edu.ihs.lhpy.domain.User;

@Controller
@Scope("prototype")
public class UserAction extends BaseAction<User> {
	private Long[] privilegeIds;

	/*
	 * 列表
	 */
	public String list() throws Exception {
//		List<User> userList = userService.findAll();
		PageBean pageBean = userService.getPageBean(pageNum);
		ActionContext.getContext().getValueStack().push(pageBean);
		return "list";
	}
	/*
	 * 添加
	 */
	public String add() throws Exception {
		// 保存到数据库种
		String passwdMD5 = DigestUtils.md5Hex("1234");
		model.setPassword(passwdMD5); // 默认密码为1234，应使用MD5摘要
		userService.save(model);
		return "toList";
	}

	/*
	 * 添加界面
	 */
	public String addUI() throws Exception {
		return "saveUI";
	}

	/*
	 * 修改
	 */
	public String edit() throws Exception {
		// 从数据库中取出原对象
		System.out.println(model.getName());
		User user = userService.getById(model.getId());
		// 修改属性，更新到数据库中
		user.setName(model.getName());
		user.setRealName(model.getRealName());
		user.setPassword(model.getPassword());
		userService.update(user);
		return "toList";
	}

	/*
	 * 修改页面
	 */
	public String editUI() throws Exception {
		User user = userService.getById(model.getId());
		// 直接放在栈顶
		ActionContext.getContext().getValueStack().push(user);
		return "saveUI";
	}

	/*
	 * 删除
	 */
	public String delete() throws Exception {
		userService.delete(model.getId());
		return "toList";
	}

	/**
	 * 初始化密码为1234
	 */
	public String initPassword() throws Exception {
		User user = userService.getById(model.getId());
		String passwdMD5 = DigestUtils.md5Hex("1234");
		user.setPassword(passwdMD5);
		userService.update(user);
		return "toList";
	}

	// ===========设置权限=================
	/** 设置权限 */
	public String setPrivilege() throws Exception {
		// 准备回显数据
		User user = userService.getById(model.getId());
		// 设置要修改的属性
		List<Privilege> privilegeList = privilegeService.getByIds(privilegeIds);
		Set<Privilege> privilege = new HashSet<Privilege>(privilegeList);
		user.setPrivileges(privilege);
		userService.update(user);
		return "toList";
	}

	/*
	 * 设置权限的页面
	 */
	public String setPrivilegeUI() throws Exception {
		// 准备数据
		User user = userService.getById(model.getId());
		ActionContext.getContext().put("user", user);
		List<Privilege> topPrivilegeList = privilegeService.findTopList();
		ActionContext.getContext().put("topPrivilegeList", topPrivilegeList);
		// 准备回显的数据
		privilegeIds = new Long[user.getPrivileges().size()];
		int index = 0;
		for (Privilege privilege : user.getPrivileges()) {
			privilegeIds[index++] = privilege.getId();
		}
		return "setPrivilegeUI";
	}

	// ====登录处理========================================================================
	/** 登录页面 */
	public String loginUI() throws Exception {
		return "loginUI";
	}

	/** 登录 */
	public String login() throws Exception {
		//验证用户名密码
		User user = userService.getByLoginNameAndPassword(model.getName(),model.getPassword());
		
		if(user == null){
			/**国际化错误消息*/
			addFieldError("login","用户名或密码错误");
			return "loginUI";
		}else{
			ActionContext.getContext().getSession().put("user",user);
			return "toIndex";
		}
	}
	/** 注销 */
	public String logout() throws Exception {
		ActionContext.getContext().getSession().remove("user");
		return "logout";
	}

	public Long[] getPrivilegeIds() {
		return privilegeIds;
	}

	public void setPrivilegeIds(Long[] privilegeIds) {
		this.privilegeIds = privilegeIds;
	}

}
